<?php

namespace app\api\controller\agent;


use app\admin\model\Shopcharge;
use app\admin\model\Shoporder;
use app\admin\model\Shopwifi;
use app\admin\model\Userorderprofit;
use app\common\controller\Api;

/**
 *
 */
class Index extends Api
{
    protected $noNeedLogin = [];
    protected $noNeedRight = ['*'];


    public function index(){

        $user = $this->auth->getUser();
        $date = $this->request->param('type');


        $shopModel = new \app\admin\model\Shop();
        $userOrderProfitModel = new Userorderprofit();
        $shopOrderModel = new Shoporder();

        $ret = array();



        $ids = array();
        $shopCountMonth = $shopModel->whereTime('create_time','month')->where('agent_id',$user['id'])->count('id');
        $shopCountAll = $shopModel->where('agent_id',$user['id'])->count('id');
        $shopCountLast = $shopModel->whereTime('create_time','last month')->where('agent_id',$user['id'])->count('id');




        $monthProfit = $userOrderProfitModel->whereTime('create_time','month')->where('user_id',$user['id'])->sum('amount');
        $lastProfit = $userOrderProfitModel->whereTime('create_time','last month')->where('user_id',$user['id'])->sum('amount');
        $allProfit = $userOrderProfitModel->where('user_id',$user['id'])->sum('amount');
        $teamProfit = $userOrderProfitModel->where('user_id',$user['id'])->where('flag','team')->sum('amount');


        $ret['shopCountMonth'] = $shopCountMonth;
        $ret['shopCountAll'] = $shopCountAll;
        $ret['shopCountLast'] = $shopCountLast;


        $ret['monthProfit'] = $monthProfit;
        $ret['lastProfit'] = $lastProfit;
        $ret['allProfit'] = $allProfit;
        $ret['teamProfit'] = $teamProfit;


        $this->success($user['id'],$ret);
    }



    public function moreList()
    {

        $startDate = $this->request->param('startDate');
        $endDate = $this->request->param('endDate');
        if($startDate == ''){
           $startDate =  date("Y-m-d",strtotime("-30 day"));
           $endDate = date("Y-m-d");
        }
        $timestamp1 = strtotime($startDate);
        $timestamp2 = strtotime($endDate);
        $days = abs($timestamp2 - $timestamp1) / (60 * 60 * 24); // 将秒转换为天

        $user = $this->auth->getUser();
        $userOrderProfitModel = new Userorderprofit();
        $list = array();
        $total = 0;
        for ($i = 1;$i<=$days;$i++){
            $date =  date("Y-m-d",strtotime("-".$i." day",strtotime($endDate)));
            $obj = array();
            $obj['date'] = $date;
            $start = $date." 00:00:00";
            $end = $date." 23:59:59";

            $profit = $userOrderProfitModel->where('user_id',$user['id'])->where('create_time',">=",strtotime($start))->where('create_time',"<=",strtotime($end))->sum('amount');
            $obj['profit'] = $profit;
            $total+= $profit;
            $list[] = $obj;
        }

        $ret = array();
        $ret['startDate'] = $startDate;
        $ret['endDate'] = $endDate;
        $ret['list'] = $list;
        $ret['total'] = $total;

        $this->success('',$ret);
    }





}
